// plugin/pages/addAddress/addAddress.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    if(e&&e.type){
      that.setData({
        type:e.type
      })
    }
    if (e && e.id) {
      that.setData({
        id: e.id
      })
    }
    if(that.data.type=='edit'){
      app.setNavtitle('编辑地址');
      wx.getStorage({
        key: 'curEditaddr',
        success: function(res) {
          console.log(res.data);
          var curAddr = res.data;
          var region = [];
          region.push(curAddr.province);
          region.push(curAddr.city);
          region.push(curAddr.area);
          that.setData({
            region: region,
            editId: curAddr.id,
            name: curAddr.name,
            mobile: curAddr.mobile,
            detail: curAddr.address,
            isDefault: curAddr.isdefault
          })
        },
        fail:function(){
          console.log("获取缓存失败");
        }
      })
    }else{
      app.setNavtitle('新增地址');
    }
  },
  inputChange:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      [type]:e.detail.value
    })
    console.log(that.data[type]);
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  saveAddress: function () {
    var that = this;
    var region = that.data.region ? that.data.region:[];
    var data = {};
    data.map = 'applet_address_add';
    data.name = that.data.name ? that.data.name:'';
    data.mobile = that.data.mobile ? that.data.mobile:'';
    data.pro = region[0] ? region[0]:'';
    data.city = region[1] ? region[1] : '';
    data.area = region[2] ? region[2] : '';
    data.address = that.data.detail ? that.data.detail:'';
    data.default = that.data.isdefault?1:0;
    var operatype = that.data.type ? that.data.type:'';
    if (operatype == 'edit') {
      data.id = that.data.editId;
    }
    if(!data.name){
      app.errorTip(that, '请输入收货人姓名', 2000);
      return;
    }
    if (!data.mobile) {
      app.errorTip(that, '请输入正确的手机号', 2000);
      return;
    }
    if (!data.pro || !data.city || !data.area) {
      app.errorTip(that, '请选择所在地区', 2000);
      return;
    }
    if (!data.address) {
      app.errorTip(that, '请输入详细地址', 2000);
      return;
    }
    //发起请求，获取列表列表
    wx.showLoading({
      title: '正在保存',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          wx.showModal({
            title: '',
            content: res.data.data.msg,
            showCancel:false,
            success: function () {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.em,
            showCancel: false
          });
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  onUnload: function () {
    wx.removeStorage({
      key: 'curEditaddr',
      success: function (res) {
        console.log("清楚编辑地址缓存成功")
      }
    })
  }
})